package TOP101;

/**
 * @Question BM10
 * @Date 2022/11/3 20:52
 * @Solution
 */
public class BM10 {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        int len1=0;
        int len2=0;
        ListNode cur1 = pHead1;
        ListNode cur2 = pHead2;
        while(cur1!=null) {
            len1++;
            cur1=cur1.next;
        }
        while(cur2!=null) {
            len2++;
            cur2=cur2.next;
        }
        cur1=pHead1;
        cur2=pHead2;
        if(len1>len2) {
            int t = len1-len2;
            for(int i=0;i<t;i++) {
                cur1=cur1.next;
            }
        } else {
            int t = len2-len1;
            for(int i=0;i<t;i++) {
                cur2=cur2.next;
            }
        }
        while(cur1!=null&&cur2!=null) {
            if(cur1==cur2) {
                return cur1;
            }
            cur1=cur1.next;
            cur2=cur2.next;
        }
        return null;
    }
}
